const {
  MarketingModel
} = require('../../../../utils/marketing-model.js')
var marketingModel = new MarketingModel()
const {
  MemberModel
} = require('../../../../utils/member-model.js');
var memberModel = new MemberModel();
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activityStatus: null, //1有活动，2.没有活动
    memberId: wx.getStorageSync('memberId') || '',
    shopId: '',
    userPrizeList: [], //用户获取的奖品
    activityPrizeList: [], //活动奖品
    acStartTime: '', //活动开始时间
    acEndTime: '', //活动结束时间
    activityRuleDesc: '', //活动规则
    currentRound: '', //活动期数
    activityCardId: '', //集卡活动ID
    activityName: '', //活动名称
    authShow: false, //登录弹出层
    shareImage: "https://oss.kiscloud.net/wechat/images/gather/collect_ac_share.png",
    lotteryFlag: null, //活动是否抽过奖：0：没有抽过奖 1：有抽过奖励
    isPrizeShow: false, //是否显示中奖详情
    loadingFlag: null, //是否正在开奖: 0：否 1：是
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      shopId: options.shopId,
      activityCardId: options.activityCardId
    }, function () {
      if (that.data.shopId == '') {
        wx.showToast({
          title: `请选择门店！`,
          icon: 'none',
          duration: 2000
        })
      } else {
        const merchantId = wx.getStorageSync('merchantId')
        if (merchantId) {
          that.getUserInfo();
        } else {
          app.getInfo().then(data => {
            that.getUserInfo();
          })
        }
      }
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    var that = this;
    if (app.globalData.isLogin) {
      memberModel.userInfo({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        that.setData({
          memberId: data.memberId || "", //会员ID
        }, function () {
          that.getActivityDetailt();
        });
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      that.setData({
        authShow: true
      })
    }
  },
  // 授权成功的回调
  loginSucceed: function () {
    this.getUserInfo()
  },

  /**
   * 活动详情
   */
  getActivityDetailt() {
    let that = this;
    const merchantId = wx.getStorageSync('merchantId')
    marketingModel.memberNationalDetail({
      merchantId: merchantId,
      memberId: that.data.memberId,
      activityCardId: that.data.activityCardId,
      shopId: that.data.shopId
    }).then(res => {
      if (res && res.nationActivityDTO) {
        let item = res.nationActivityDTO || []
        let activityStart = item.activityStart.split(' ')
        let atime = activityStart[0].split('-')
        let activityEnd = item.activityEnd.split(' ')
        let etime = activityEnd[0].split('-')
        let userPrizeData = (res.nationActivityRewardDTOList || []).map((item, index) => {
          let phone = item.phoneNumber ? item.phoneNumber.substr(0, 3) + '****' + item.phoneNumber.substr(7, 4) : ''
          return {
            ...item,
            phoneNumber: phone
          }
        })
        if (res.nationActivityRewardDTOList && res.nationActivityRewardDTOList.length > 0) {
          that.getActivityInfo();
        } else {
          that.setData({
            isPrizeShow: false
          })
        }
        that.setData({
          userPrizeList: userPrizeData || [],
          acStartTime: `${atime[0]}年${atime[1]}月${atime[2]}日` + `${activityStart[1]}` || '',
          acEndTime: `${etime[0]}年${etime[1]}月${etime[2]}日` + `${activityEnd[1]}` || '',
          activityPrizeList: item.activityAwardDetailList || [],
          currentRound: item.currentRound || '',
          activityRuleDesc: item.activityRuleDesc || '',
          activityCardId: item.activityCardId || '',
          activityName: item.activityName || '',
          activityStatus: 1, //1有活动，2.没有活动
          lotteryFlag: item.lotteryFlag || null, //活动是否抽过奖：0：没有抽过奖 1：有抽过奖励
          loadingFlag: item.loadingFlag || null //是否正在开奖: 0：否 1：是
        })
        wx.setNavigationBarTitle({
          title: item.activityName || ""
        })
      } else {
        that.setData({
          activityStatus: 2
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 会员活动信息
   */
  getActivityInfo: function () {
    let that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户ID
    param.activityVerificationId = ''; //核销ID
    param.activityCardType = 2; //活动类型：1：集卡活动一期 2：全国活动 3：达人机活动
    param.activityCardId = that.data.activityCardId; //活动id
    marketingModel.verificationGet(param).then(data => {
      if (data) {
        let currentRecord = data.currentRecord || []
        let historyRecord = data.historyRecord || []
        if (currentRecord.length > 0 || historyRecord.length > 0) {
          that.setData({
            isPrizeShow: true
          })
        } else {
          that.setData({
            isPrizeShow: false
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 跳转到活动详情
   */
  goPrizeInfo: function () {
    wx.navigateTo({
      url: '/pages/activity/collectCards/prizeInfo/index?activityCardId=' + this.data.activityCardId
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.activityName || "",
      imageUrl: this.data.shareImage,
      path: `pages/activity/collectCards/prizeDraw/index?shopId=${this.data.shopId}&activityCardId=${this.data.activityCardId}&shareMemberId=${this.data.memberId}&activityId=${this.data.activityCardId}&isShare=1`,
    }
  }
})